from django.shortcuts import render
from django.http import HttpResponse
import json
import save_data
# Create your views here.

def hello(request):
    return HttpResponse("hello from share app")

def main(request,page):
    items=json.dumps([
        {'name':'heyulin',
         'href':'http://www.sina.com.cn'},
        {'name':'zhangrong',
         'href':'http://www.baidu.com'}
        ])
    return render(request,'share/index.html',context={'page':page,'items':items})

def record(request,user):
    pass

def test_page(request,page):
    if page =='main':
        items=json.dumps([
            {'name':'heyulin',
             'link':'http://www.sina.com.cn'},
            {'name':'zhangrong',
             'link':'http://www.baidu.com'}
        ])
        dc ={
            'page':page,
            'items':items
        }
        return render(request,"share/index.html",context = dc)
    elif page == 'ng':
        return render(request,"share/ng.html")
    
    elif page=='postjson':
        dc= json.loads(request.body)
        if dc.pop('operation') =='save':
            save_data.save_record(dc)
        return HttpResponse('hello')
    elif page=='getjson':
        response_data=[
            # {"name":'heyulin','age':34},
            # {"name":"zhangrong",'age':30}
            ['heyulin',34],
            ['zhangrong',30]
        ]
        return HttpResponse(json.dumps(response_data), content_type="application/json")



    